﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using XCI.Core;
using XCI.Data.Entity;
using XCI.Data.Expressions;
using XCI.Extensions;

namespace XCI.Data.Expressions
{
    /// <summary>
    /// 查询语句扩展
    /// </summary>
    public static class SQLinqExtensions
    {
        /// <summary>
        /// Between查询
        /// </summary>
        /// <param name="column">列名</param>
        /// <param name="start">开始值</param>
        /// <param name="end">结束值</param>
        public static bool Between(this object column, object start, object end)
        {
            return true;
        }

        /// <summary>
        /// In查询(使用Or实现)
        /// </summary>
        /// <param name="column">列名</param>
        /// <param name="values">列值</param>
        public static bool OrArrayString(this object column, string[] values)
        {
            return true;
        }

        /// <summary>
        /// In查询(非参数化)
        /// </summary>
        /// <param name="column">列名</param>
        /// <param name="values">列值</param>
        public static bool InArrayString(this object column, string[] values)
        {
            return true;
        }

        /// <summary>
        /// In查询
        /// </summary>
        /// <param name="column">列名</param>
        /// <param name="values">列值</param>
        public static bool In(this object column, string[] values)
        {
            return true;
        }

        /// <summary>
        /// In查询
        /// </summary>
        /// <param name="column">列名</param>
        /// <param name="values">列值</param>
        public static bool In(this object column, int[] values)
        {
            return true;
        }

        /// <summary>
        /// In查询
        /// </summary>
        /// <param name="column">列名</param>
        /// <param name="values">列值</param>
        public static bool In(this int column, string[] values)
        {
            return true;
        }

        /// <summary>
        /// NotIn查询
        /// </summary>
        /// <param name="column">列名</param>
        /// <param name="values">列值</param>
        public static bool NotIn(this object column, string[] values)
        {
            return true;
        }

        /// <summary>
        /// NotIn查询
        /// </summary>
        /// <param name="column">列名</param>
        /// <param name="values">列值</param>
        public static bool NotIn(this object column, int[] values)
        {
            return true;
        }

        /// <summary>
        /// 创建查询表达式
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="pagination">分页参数</param>
        /// <param name="defaultSort">默认排序字段（如果分页对象没有指定分页参数时启用）</param>
        /// <param name="defaultOrder">默认排序类型（如果分页对象没有指定分页参数时启用）</param>
        /// <returns></returns>
        public static SQLQuery<T> ToSQLQuery<T>(this Pagination pagination, string defaultSort = null, 
            OrderDir defaultOrder = OrderDir.Desc)
        {
            var sql = new SQLQuery<T>();
            sql.Take(pagination.rows).Page(pagination.page);
            var sort = defaultSort ?? string.Empty;
            var order = defaultOrder.ToString().ToLower() ?? string.Empty;
            if (pagination.sort.IsNotEmpty())
            {
                sort = pagination.sort;
            }
            if (pagination.order.IsNotEmpty())
            {
                order = pagination.order;
            }
            if (sort.IsEmpty())
            {
                return sql;
            }
            var sorts = sort.Split(new[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);
            var orders = order.Split(new[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);
            if (sorts.Length != orders.Length)
            {
                orders = new string[sorts.Length];
                for (int i = 0; i < sorts.Length; i++)
                {
                    orders[i] = "asc";
                }
            }
            var meta = EntityHelper.Get<T>();
            for (int i = 0; i < sorts.Length; i++)
            {
                var sname = sorts[i];
                if(meta.AllColumns.TryGetValue(sname,out EntityColumn col))
                {
                    sql.OrderBy(col.DbName,orders[i].IsAsc());
                } 
            }
            return sql;
        }
    }
}